package com.yaowk.service.mall.clothing.service.impl;

import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.api.mall.clothing.model.Surveyor;
import com.yaowk.api.mall.clothing.service.ISurveyorService;
import com.yaowk.api.mall.clothing.web.SurveyorModel;
import com.yaowk.api.utils.DateUtils;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.mall.clothing.dao.MemberDao;
import com.yaowk.service.mall.clothing.dao.SurveyorDao;
import com.yaowk.service.mall.clothing.entity.CmSurveyor;
import com.yaowk.service.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SurveyorServiceImpl extends BaseServiceImpl<Surveyor, CmSurveyor, Integer> implements ISurveyorService {

    @Autowired
    private SurveyorDao dao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private DateUtils dateUtils;
    @Autowired
    private PageUtils pageUtils;

    @Override
    public PageResp<SurveyorModel> findAllOfWeb(String keyword, PageReq pageReq) {
        // 查询列表
        PageRequest pageRequest = pageUtils.pageReq2PageRequest(pageReq);
        CmSurveyor condition = new CmSurveyor();
        if (isNotEmpty(keyword)) {
            keyword = "%" + keyword + "%";
            condition.setPhoneNumber(keyword);
            condition.setRealName(keyword);
        }
        Page<CmSurveyor> page = dao.findAll(Example.of(condition), pageRequest);

        // 构造返回
        PageResp<SurveyorModel> pageResp = pageUtils.page2PageResp(page);

        // 计算时间区间
        Date now = new Date();
        Date todayBegin = dateUtils.getBeginTimeOfDay(now);
        Date todayEnd = dateUtils.getEndTimeOfDay(now);
        Date monthBegin = dateUtils.getBeginTimeOfMonth(now);
        Date monthEnd = dateUtils.getEndTimeOfMonth(now);

        // 封装返回内容
        List<SurveyorModel> memberModels = pageResp.getList();
        for (CmSurveyor cmSurveyor : page.getContent()) {
            SurveyorModel surveyorModel = entity2model(cmSurveyor);
            memberModels.add(surveyorModel);
            surveyorModel.setInputNumMonth(memberDao.countByInputTimeBetweenAndSurveyorId(monthBegin, monthEnd, cmSurveyor.getId()));
            surveyorModel.setInputNumToday(memberDao.countByInputTimeBetweenAndSurveyorId(todayBegin, todayEnd, cmSurveyor.getId()));
        }

        return pageResp;
    }

    @Override
    public SurveyorModel findDetailById(Integer id) {
        CmSurveyor cmSurveyor = dao.findOne(id);
        SurveyorModel surveyorModel = entity2model(cmSurveyor);

        // 计算时间区间
        Date now = new Date();
        Date todayBegin = dateUtils.getBeginTimeOfDay(now);
        Date todayEnd = dateUtils.getEndTimeOfDay(now);
        Date monthBegin = dateUtils.getBeginTimeOfMonth(now);
        Date monthEnd = dateUtils.getEndTimeOfMonth(now);

        // 统计激活和新增会员数
        surveyorModel.setActiveNumToday(memberDao.countByActiveTimeBetweenAndSurveyorId(todayBegin, todayEnd, id));
        surveyorModel.setActiveNumMonth(memberDao.countByActiveTimeBetweenAndSurveyorId(monthBegin, monthEnd, id));
        surveyorModel.setInputNumToday(memberDao.countByInputTimeBetweenAndSurveyorId(todayBegin, todayEnd, id));
        surveyorModel.setInputNumMonth(memberDao.countByInputTimeBetweenAndSurveyorId(monthBegin, monthEnd, id));
        return surveyorModel;
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected CmSurveyor model2Entity(Surveyor model) {
        CmSurveyor cmSurveyor = null;
        if (isNotEmpty(model)) {
            cmSurveyor = new CmSurveyor();
            cmSurveyor.setUsername(model.getUsername());
            cmSurveyor.setStatus(model.getStatus());
            cmSurveyor.setPassword(model.getPassword());
            cmSurveyor.setParentId(model.getParentId());
            cmSurveyor.setId(model.getId());
            cmSurveyor.setCreateTime(model.getCreateTime());
            cmSurveyor.setSex(model.getSex());
            cmSurveyor.setRealName(model.getRealName());
            cmSurveyor.setPhoneNumber(model.getPhoneNumber());
            cmSurveyor.setHeadUrl(model.getHeadUrl());
            cmSurveyor.setCommissionId(model.getCommissionId());
            cmSurveyor.setCode(model.getCode());
            cmSurveyor.setAccount(model.getAccount());
        }
        return cmSurveyor;
    }

//    @Override
//    protected Surveyor entity2model(CmSurveyor entity) {
//        Surveyor surveyor = null;
//        if (isNotEmpty(entity)) {
//            surveyor = new Surveyor();
//            surveyor.setUsername(entity.getUsername());
//            surveyor.setStatus(entity.getStatus());
//            surveyor.setPassword(entity.getPassword());
//            surveyor.setParentId(entity.getParentId());
//            surveyor.setId(entity.getId());
//            surveyor.setCreateTime(entity.getCreateTime());
//            surveyor.setSex(entity.getSex());
//            surveyor.setRealName(entity.getRealName());
//            surveyor.setPhoneNumber(entity.getPhoneNumber());
//            surveyor.setHeadUrl(entity.getHeadUrl());
//            surveyor.setCommissionId(entity.getCommissionId());
//            surveyor.setCode(entity.getCode());
//            surveyor.setAccount(entity.getAccount());
//        }
//        return surveyor;
//    }

    @Override
    protected SurveyorModel entity2model(CmSurveyor entity) {
        SurveyorModel surveyorModel = null;
        if (isNotEmpty(entity)) {
            surveyorModel = new SurveyorModel();
            surveyorModel.setUsername(entity.getUsername());
            surveyorModel.setStatus(entity.getStatus());
            surveyorModel.setPassword(entity.getPassword());
            surveyorModel.setParentId(entity.getParentId());
            surveyorModel.setId(entity.getId());
            surveyorModel.setCreateTime(entity.getCreateTime());
            surveyorModel.setSex(entity.getSex());
            surveyorModel.setRealName(entity.getRealName());
            surveyorModel.setPhoneNumber(entity.getPhoneNumber());
            surveyorModel.setHeadUrl(entity.getHeadUrl());
            surveyorModel.setCommissionId(entity.getCommissionId());
            surveyorModel.setCode(entity.getCode());
            surveyorModel.setAccount(entity.getAccount());
        }
        return surveyorModel;
    }
}
